<?php

    #-------------------------------------------------------------#
	# This file is part of the Tekuna MVC Microframework for PHP. #
	# (c) 2009 Niels Jäckel <niels.jaeckel@tekuna.org>            #
	# Please visit http://tekuna.org/ for more information.       #
	#                                                             #
	# This software is licensed under the MIT License. Please     #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#


	Tekuna :: load('org.tekuna.core.application.TekunaApplicationContext');


	class ApplicationContextTest extends AbstractTekunaTestCase {

		public function testGettersSetters() {

			// create context
			$objContext = new TekunaApplicationContext();

			// test default values
			$this -> assertNull($objContext -> getRequest());
			$this -> assertNull($objContext -> getResponse());
			$this -> assertNull($objContext -> getClientInformation());
			$this -> assertNull($objContext -> getURLInformation());
			$this -> assertNull($objContext -> getApplicationConfiguration());
			$this -> assertIdentical($objContext -> getApplicationLanguage(), '');
			$this -> assertIdentical($objContext -> getAllContextValues(), array());
			$this -> assertIdentical($objContext -> getLayoutValues(), array('applicationLanguage' => ''));

			// test request
			$objRequest = new Request();
			$objContext -> setRequest($objRequest);
			$this -> assertIdentical($objContext -> getRequest(), $objRequest);

			// test response
			$objResponse = new Response();
			$objContext -> setResponse($objResponse);
			$this -> assertIdentical($objContext -> getResponse(), $objResponse);

			// test client information
			$objClient = new ClientInformation();
			$objContext -> setClientInformation($objClient);
			$this -> assertIdentical($objContext -> getClientInformation(), $objClient);

			// test url information
			$objURLInformation = new URLInformation();
			$objContext -> setURLInformation($objURLInformation);
			$this -> assertIdentical($objContext -> getURLInformation(), $objURLInformation);

			// test language
			$sLanguage = 'fr';
			$objContext -> setApplicationLanguage($sLanguage);
			$this -> assertIdentical($objContext -> getApplicationLanguage(), $sLanguage);
			$this -> assertIdentical($objContext -> getLayoutValues(), array('applicationLanguage' => 'fr'));

			// test application configuration
			$objConfig = new ApplicationConfiguration();
			$objContext -> setApplicationConfiguration($objConfig);
			$this -> assertIdentical($objContext -> getApplicationConfiguration(), $objConfig);

			// test language exception
			try {

				// application language setting will result in an exception
				// because the configuration does not contain any language
				$objContext -> setApplicationLanguage($sLanguage);
				$this -> fail();
			}
			catch (ApplicationContextException $e) {

				$this -> pass();
			}

			// add supported language and test again
			$objConfig -> setLanguages(array('en', 'fr'));
			$objContext -> setApplicationLanguage('fr');
			$this -> assertIdentical($objContext -> getApplicationLanguage(), 'fr');
			$objContext -> setApplicationLanguage('en');
			$this -> assertIdentical($objContext -> getApplicationLanguage(), 'en');

			// test language exception
			try {

				// application language setting will result in an exception
				// because the configuration does not contain any language
				$objContext -> setApplicationLanguage('de');
				$this -> fail();
			}
			catch (ApplicationContextException $e) {

				$this -> pass();
			}

			// test context values
			$sKey = 'contextKey';
			$sValue = 'contextValue';

			$objContext -> setContextValue($sKey, $sValue);
			$this -> assertIdentical($objContext -> getContextValue($sKey), $sValue);
			$this -> assertIdentical($objContext -> getAllContextValues(), array($sKey => $sValue));

			$this -> assertTrue($objContext -> contextValueExists($sKey));
			$this -> assertFalse($objContext -> contextValueExists('nonExistentValue'));

			// test exception with non-existent context values
			try {

				$objContext -> getContextValue('nonExistentValue');
				$this -> fail();
			}
			catch (ApplicationContextException $e) {

				$this -> pass();
			}

			$this -> assertIdentical($objContext -> getLayoutValues(), array($sKey => $sValue, 'applicationLanguage' => 'en'));
		}
	}



